package report3;

import report3.classes.Page;
import report3.classes.Paragraph;

import java.util.Map;
import java.util.Scanner;

/**
 * Author：zouran
 * Date：2023/5/4  15:12
 */
public class Main {
    public static void page_exe() {
        Page page = new Page();
        System.out.println("请输入每页大小：（0-1K；1-2K；2-4K；3-随机选择）");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                page.setPageSize(2);
                break;
            case 2:
                page.setPageSize(4);
                break;
            case 3:
                page.setRandPageSize();
                break;
            case 0:
            default:
                page.setPageSize(1);
                break;
        }
        System.out.println("请输入页表项个数：（5到10之间整数，含端点）");
        int count = scanner.nextInt();
        page.setCount(count);
        System.out.println("请输入页表中每页对应的块号：（0-手动输入；1-随机生成0至10之间的非重复整数作为块号）");
        choice = scanner.nextInt();
        switch (choice) {
            case 0: {
                page.readScannerMap();
                break;
            }
            case 1:
            default:
                page.randMap();
                break;
        }
        System.out.println("您选择了分页方式，输入的数据如下：");
        System.out.printf("每页大小%dK，页表为\n", page.getPageSize());
        System.out.println("页号\t块号");
        page.printMap();
        System.out.println("请开始你的表演（输入逻辑地址）");
        while (true) {
            int address = scanner.nextInt();
            Map<String, Integer> hashMap = page.calculate(address);
            if (hashMap.get("status") == 1) {
                System.out.printf("""
                                您选择了分页方式，输入的逻辑地址为%dk，其对应的页号为%d，页偏移为%dk;
                                该地址对应的块号为%d，块偏移为%dk，物理地址为%dk。
                                """,
                        address, address / page.getPageSize(), address % page.getPageSize(),
                        hashMap.get("page"), hashMap.get("offset"), hashMap.get("position"));
                break;
            } else if (hashMap.get("status") == 0) {
                System.out.println("您输入的地址已溢出，请重新输入！");
            }
        }
    }

    public static void paragraph_exe() {
        Paragraph paragraph = new Paragraph();
        System.out.println("请输入段表项个数：（5到10之间整数，含端点）");
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        paragraph.setCount(count);
        System.out.println("请输入页表中每段对应的基址和段长：（0-手动输入；1-随机生成）");
        int choice = scanner.nextInt();
        switch (choice) {
            case 0: {
                paragraph.readScannerMap();
                break;
            }
            case 1:
            default: {
                paragraph.setRandParagraph();
                break;
            }
        }
        System.out.println("  您选择了分段方式，输入的数据如下：");
        paragraph.printMap();
        System.out.println("请开始你的表演（输入逻辑地址）");
        while (true) {
            int address = scanner.nextInt();
            Map<String, Integer> hashMap = paragraph.calculate(address);
            if (hashMap.get("status") == 1) {
                System.out.printf("""
                                您选择了分段方式，输入的逻辑地址为%dk，其对应的段号为%d，段偏移为%dk；
                                该段对应的基址为%dk，段偏移为%dk，物理地址为%dk。
                                """,
                        address, hashMap.get("paragraph"), hashMap.get("offset"), hashMap.get("begin"), hashMap.get("offset"), hashMap.get("position"));
                break;
            } else if (hashMap.get("status") == 0) {
                System.out.println("您输入的地址已溢出，请重新输入！");
            }
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要选择的地址变化方式：（0-分页；1-分段）");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    paragraph_exe();
                    break;
                case 0:
                    page_exe();
                    break;
            }
            if (choice == -1) break;
        }
    }
}